package com.bruno.tecnoagenda;

import com.bruno.tecnoagenda.data.PersonContainer;
import com.bruno.tecnoagenda.ui.HelpView;
import com.bruno.tecnoagenda.ui.SearchFilter;
import com.bruno.tecnoagenda.ui.SearchView;
import com.bruno.tecnoagenda.ui.ShareView;
import com.vaadin.Application;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

/**
 * Corpo da interface de aplicação
 * 
 * @author bssantos
 * 
 */
public class TecnoAgenda extends Application implements ClickListener,
		ValueChangeListener, ItemClickListener {

	private Button newContact = new Button("Adicionar Contato");
	private Button search = new Button("Procurar");
	private Button share = new Button("Compartilhar");
	private Button help = new Button("Ajuda");
	private SplitPanel horizontalSplit = new SplitPanel(
			SplitPanel.ORIENTATION_HORIZONTAL);
	private NavigationTree tree = new NavigationTree(this);
	private ListView listView = null;
	private PersonList personList = null;
	private PersonForm personForm = null;
	private HelpWindow helpWindow = null;
	private SharingOptions sharingOptions = null;
	private PersonContainer dataSource = PersonContainer.createWithTestData();
	private SearchView searchView = null;
	private HelpView helpView = null;
	private ShareView shareView = null;

	@Override
	public void init() {
		buildMainLayout();
		setMainComponent(getListView());	
	}

	private void buildMainLayout() {
		setTheme("contacts");
		setMainWindow(new Window("TecnoAgenda"));
		VerticalLayout layout = new VerticalLayout();
		layout.setSizeFull();

		layout.addComponent(createToolbar());
		layout.addComponent(horizontalSplit);
		/* Allocate all available extra space to the horizontal split panel */
		layout.setExpandRatio(horizontalSplit, 1);
		/*
		 * Set the initial split position so we can have a 200 pixel menu to the
		 * left
		 */
		horizontalSplit.setSplitPosition(200, SplitPanel.UNITS_PIXELS);
		getMainWindow().setContent(layout);
		getMainWindow().addWindow(getHelpWindow());
		getMainWindow().addWindow(getSharingOptions());
		horizontalSplit.setFirstComponent(tree);
		
		
		
	}

	public HorizontalLayout createToolbar() {
		HorizontalLayout lo = new HorizontalLayout();
		lo.addComponent(newContact);
		newContact.setIcon(new ThemeResource("icons/32/document-add.png"));
		search.addListener((Button.ClickListener) this);
		search.setIcon(new ThemeResource("icons/32/folder-add.png"));
		lo.addComponent(search);
		share.addListener((Button.ClickListener) this);
		share.setIcon(new ThemeResource("icons/32/users.png"));
		lo.addComponent(share);
		help.addListener((Button.ClickListener) this);
		help.setIcon(new ThemeResource("icons/32/help.png"));
		lo.addComponent(help);
		//editando estilo visual
        lo.setMargin(true);
        lo.setSpacing(true);
        //imagem de logotipo
/*        lo.setWidth("100%");
        Embedded em = new Embedded("", new ThemeResource("images/avontade_header_img.jpg"));
        lo.addComponent(em);
        lo.setComponentAlignment(em, Alignment.MIDDLE_RIGHT);
        lo.setExpandRatio(em, 1);*/
        //tema da toolbar
        lo.setStyleName("toolbar");
        
		return lo;
	}

	private void setMainComponent(Component c) {
		horizontalSplit.setSecondComponent(c);
	}

	private ListView getListView() {
		if (listView == null) {
			personList = new PersonList(this);
			personForm = new PersonForm(this);
			listView = new ListView(personList, personForm);
		}
		return listView;
	}

	public HelpWindow getHelpWindow() {
		if (this.helpWindow == null) {
			this.helpWindow = new HelpWindow();
		}
		return helpWindow;
	}

	public SharingOptions getSharingOptions() {
		if (this.sharingOptions == null) {
			this.sharingOptions = new SharingOptions();
		}
		return sharingOptions;
	}

	public PersonContainer getDataSource() {
		return dataSource;
	}

	private SearchView getSearchView() {
		if (searchView == null) {
			searchView = new SearchView(this);
		}
		return searchView;
	}

	private HelpView getHelpView() {
		if (helpView == null) {
			helpView = new HelpView(this);
		}
		return helpView;
	}

	private ShareView getShareView() {
		if (shareView == null) {
			shareView = new ShareView(this);
		}
		return shareView;
	}

	private void showSearchView() {
		setMainComponent(getSearchView());
	}

	private void showHelpView() {
		setMainComponent(getHelpView());
	}

	private void showShareView() {
		setMainComponent(getShareView());
	}

	public void buttonClick(ClickEvent event) {

		final Button source = event.getButton();

		if (source == search) {
			showSearchView();
		}

		if (source == help) {
			showHelpView();
		}

		if (source == share) {
			showShareView();
		}

	}

	public void valueChange(ValueChangeEvent event) {
		Property property = event.getProperty();
		if (property == personList) {
			Item item = personList.getItem(personList.getValue());
			if (item != personForm.getItemDataSource()) {
				personForm.setItemDataSource(item);
			}
		}
	}

	private void showListView() {
		setMainComponent(getListView());
	}

	public void itemClick(ItemClickEvent event) {
		Object itemId = event.getItemId();
		if (event.getSource() == tree) {

			if (itemId != null) {
				if (itemId == NavigationTree.SHOW_ALL) {
					// clear previous filters
					getDataSource().removeAllContainerFilters();
					showListView();
				} else if (itemId == NavigationTree.SEARCH) {
					showSearchView();
				} else if (itemId instanceof SearchFilter) {
					search((SearchFilter) itemId);
				}
			}
		} else if (itemId instanceof SearchFilter) {
			search((SearchFilter) itemId);
		}

	}

	private void addNewContanct() {
		showListView();
		personForm.addContact();
	}

	public void search(SearchFilter searchFilter) {
		
		//mostrando notificação de pesquisa
		getMainWindow().showNotification(
		         "Procura feita por " + searchFilter.getPropertyId() + "=*"
		         + searchFilter.getTerm() + "*, encontrado "
		         + getDataSource().size() + " iten(s).",
		         Notification.TYPE_TRAY_NOTIFICATION);
		
		// clear previous filters
		getDataSource().removeAllContainerFilters();
		// filter contacts with given filter
		getDataSource().addContainerFilter(searchFilter.getPropertyId(),
				searchFilter.getTerm(), true, false);
		showListView();
	}

	public void saveSearch(SearchFilter searchFilter) {
		tree.addItem(searchFilter);
		tree.setParent(searchFilter, NavigationTree.SEARCH);
		// mark the saved search as a leaf (cannot have children)
		tree.setChildrenAllowed(searchFilter, false);
		// make sure "Search" is expanded
		tree.expandItem(NavigationTree.SEARCH);
		// select the saved search
		tree.setValue(searchFilter);
	}

}
